" _____           __     ___
"|__  /______  __ \ \   / (_)_ __ ___
"  / /|_  /\ \/ /  \ \ / /| | '_ ` _ \
" / /_ / /  >  <    \ V / | | | | | | |
"/____/___|/_/\_\    \_/  |_|_| |_| |_|
"
" for ssh
" let &t_SI = "\<Esc>]50;CursorShape=1\x7"
" let &t_SR = "\<Esc>]50;CursorShape=2\x7"
" let &t_EI = "\<Esc>]50;CursorShape=0\x7"

let g:KEYBORD_TYPE = "qwer"
" let g:KEYBORD_TYPE = "colemak"
let mapleader=" "
" function! MySys()
"     if has("win32")
"         return "windows"
"     else
"         return "linux"
"     endif
" endfunction

let g:plug_file_path = "~/.config/nvim/plugged"
let g:VIMPATH = "~/.config/nvim"

" set foldlevelstart=0
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1,gbk,gb2312
" set shortmess+=c
" set termencoding=utf-8
set fileencoding=utf-8
set timeoutlen=3000
"set shell=powershell.exe

filetype on
filetype indent on
filetype plugin on
filetype plugin indent on

set noswapfile
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set scrolloff=99
set sidescrolloff=6
set tw=0
set indentexpr=
set backspace=indent,eol,start
"set foldmethod=indent
"set foldlevel=99
set foldmethod=marker
set laststatus=2
set autochdir
" set shortmess=atl
set formatoptions=tcrqn
" set guifont=FiraCode\ Nerd\ Font,\ Microsoft\ YaHei,\ monospace

set guifont=Fira\ Code:h14
set guicursor=n-v-c:block,i-ci-ve:ver25,r-cr:hor20,o:hor50
set guicursor=n-v-c:block-Cursor/lCursor,ve:ver35-Cursor,o:hor50-Cursor,i-ci:ver25-Cursor/lCursor,r-cr:hor20-Cursor/lCursor,sm:block-Cursor-blinkwait0-blinkoff0-blinkon0,a:blinkon0
		  " \,a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor
		  " \,sm:block-blinkwait175-blinkoff150-blinkon175

au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

let &t_ut=''
set nocompatible
set number
" set relativenumber
if has('nvim')
    syntax on
endif
set wrap
set linebreak
set showcmd

if has('nvim')
    set noshowmode
endif

set wildmenu
set hlsearch
let loaded_matchparen = 0
set incsearch
set ignorecase
set smartcase
set clipboard+=unnamed,unnamedplus
set clipboard+=unnamedplus
set mouse=
set cursorline
"set cursorcolumn
set cmdheight=1
set noerrorbells
set showmatch
"set visualbell
set autoindent
set smartindent
set cindent
set pyxversion=3
"set pythonthreedll=python39.dll
"set fillchars=vert:\,stl:\,stlnc:\
set hidden
" set tags=tags
" set tags+=./tags
" set tags+=./tags,./../tags,./../../tags
" set path+=./*,./../*,./../../*
" set cscopetag " 使用 cscope 作为 tags 命令
" set cscopeprg='gtags-cscope' " 使用 gtags-cscope 代替 cscope


"gtags.vim 设置项
" let GtagsCscope_Auto_Load = 1
" let CtagsCscope_Auto_Map = 1
" let GtagsCscope_Quiet = 1

" exec 'nohl'

" if has("nvim")
" autocmd TermOpen * startinsert
" endif

autocmd WinLeave * setlocal nocursorline
" autocmd WinEnter * setlocal cursorline
autocmd BufEnter * setlocal cursorline
autocmd BufEnter,BufRead,BufNewFile *.md set filetype=markdown
autocmd BufEnter,BufRead,BufNewFile *.md set fdm=marker
autocmd BufEnter,BufRead,BufNewFile vifmrc set filetype=vim
autocmd BufEnter,BufRead,BufNewFile *.md set nocindent
" U<C-j> for both expand and jump (make expand higher priority.)

noremap <nowait> <LEADER>nn :call Compile()<CR>
noremap <LEADER>jk :call CompileRun()<CR>
noremap <LEADER>kk :call ZzxRun()<CR>
noremap <LEADER>kj :call ZzxRedirectRun()<CR>
noremap <LEADER>hh :call Debug()<CR>

let curdir = substitute(getcwd(), $HOME, "~", "g")
let g:file_whole_name = @%

if match(g:file_whole_name,'^.\{-}\.md') == -1
    autocmd BufWritePre * :%s/\s\+$//e
endif

let file_exe_name = expand('%<').'.exe'
" let g:file_exe_name = expand('%<')
let g:file_name = expand('%:t<')

func! Compile()
    let name=g:file_whole_name
    if &filetype == 'cpp'
        exec 'tabe'
        exec 'term g++ -g '.name.' -o ~/Documents/Programming/Cpp/ctmp.exe'
    elseif &filetype == 'c'
        exec 'tabe'
        exec 'term gcc '.name.' -o ~/Documents/Programming/Cpp/ctmp.exe'
    elseif &filetype == 'go'
        exec 'tabe'
        exec 'term go build'
    "" call system('st -e sh -c \"g++ ' .g:file_name. ' -o ~/Documents/cpptmp.exe; read a"')
  endif
endfunc

function! Debug()
    if &filetype == 'cpp'
        " call system('st -e sh -c \"~/Documents/cpptmp.exe; echo; echo; echo [process end]; read a"')
        exec "tabe"
        exec "term gdb ~/Documents/Programming/Cpp/ctmp.exe"
    elseif &filetype == 'c'
        exec "term cat input.txt | ~/Documents/Programming/Cpp/ctmp.exe"
        " call system('st -e sh -c \"~/Documents/ctmp.exe; echo; echo; echo [process end]; read a"')
    elseif &filetype == 'java'
       exec "!javac %"
       exec "!time java %<"
    elseif &filetype == 'asm'
       exec "!lc3as %<"
    elseif &filetype == 'sh'
        exec "tabe %"
        :term time bash %
   elseif &filetype == 'python'
     set splitbelow
     :sp
     :term python3 %
   elseif &filetype == 'html'
     silent! exec "!".g:mkdp_browser." % &"
   elseif &filetype == 'markdown'
       silent exec "!google-chrome-stable %"
     " exec "InstantMarkdownPreview"
   elseif &filetype == 'tex'
     silent! exec "VimtexStop"
     silent! exec "VimtexCompile"
   elseif &filetype == 'dart'
     exec "CocCommand flutter.run -d ".g:flutter_default_device." ".g:flutter_run_args
     silent! exec "CocCommand flutter.dev.openDevLog"
   elseif &filetype == 'javascript'
     set splitbelow
     :sp
     :term export DEBUG="INFO,ERROR,WARNING"; node --trace-warnings .
   elseif &filetype == 'go'
       " call system('st -e sh -c \"go run '.@%.'; echo; echo [ Process exit ]; read a"')
       exec 'tabe'
       exec 'term cat input.txt | go run '.g:file_whole_name
  endif
endfunc

function! ZzxRedirectRun()
    if &filetype == 'cpp'
        " call system('st -e sh -c \"~/Documents/cpptmp.exe; echo; echo; echo [process end]; read a"')
        exec "tabe"
        exec "term cat input.txt | ~/Documents/Programming/Cpp/ctmp.exe"
    elseif &filetype == 'c'
        exec "term cat input.txt | ~/Documents/Programming/Cpp/ctmp.exe"
        " call system('st -e sh -c \"~/Documents/ctmp.exe; echo; echo; echo [process end]; read a"')
    elseif &filetype == 'java'
        exec "tabe %"
        exec "term javac % && cat input.txt | time java %<"
    elseif &filetype == 'asm'
       exec "!lc3as %<"
    elseif &filetype == 'sh'
     :!time bash %
   elseif &filetype == 'python'
       exec "tabe %"
       exec "term cat input.txt | python %"
   elseif &filetype == 'html'
     silent! exec "!".g:mkdp_browser." % &"
   elseif &filetype == 'markdown'
       silent exec "!google-chrome-stable %"
     " exec "InstantMarkdownPreview"
   elseif &filetype == 'tex'
     silent! exec "VimtexStop"
     silent! exec "VimtexCompile"
   elseif &filetype == 'dart'
     exec "CocCommand flutter.run -d ".g:flutter_default_device." ".g:flutter_run_args
     silent! exec "CocCommand flutter.dev.openDevLog"
   elseif &filetype == 'javascript'
     set splitbelow
     :sp
     :term export DEBUG="INFO,ERROR,WARNING"; node --trace-warnings .
   elseif &filetype == 'go'
       " call system('st -e sh -c \"go run '.@%.'; echo; echo [ Process exit ]; read a"')
       exec 'tabe'
       exec 'term cat input.txt | go run '.g:file_whole_name
  endif
endfunc

function! CompileRun()
    if &filetype == 'cpp'
        " call system('st -e sh -c \"~/Documents/cpptmp.exe; echo; echo; echo [process end]; read a"')
        exec "tabe %"
        exec 'term g++ -g % -o ~/Documents/Programming/Cpp/ctmp.exe && ~/Documents/Programming/Cpp/ctmp.exe'
    elseif &filetype == 'c'
        exec "tabe %"
        exec 'term gcc -g % -lm -o ~/Documents/Programming/Cpp/ctmp.exe && ~/Documents/Programming/Cpp/ctmp.exe'
        " exec "tabe"
        " exec "term ~/Documents/Programming/Cpp/ctmp.exe"
        " " call system('st -e sh -c "~/Documents/ctmp.exe; echo; echo; echo [process end]; read a"')
    elseif &filetype == 'java'
        :tabe %
        exec "term javac % && java %<"
    elseif &filetype == 'asm'
        exec "!lc3as %<"
    elseif &filetype == 'sh'
        exec "tabe %"
        :term time bash %
    elseif &filetype == 'python'
        " set splitbelow
        " :sp
        exec "tabe %"
        :term python3 %
     " :term python3 %
    elseif &filetype == 'make'
        exec "tabe %"
        exec 'term make'
    elseif &filetype == 'html'
        silent exec "!microsoft-edge-stable %"
    elseif &filetype == 'latex'
        silent exec "!microsoft-edge-stable %"
    elseif &filetype == 'markdown'
        silent exec "!microsoft-edge-stable %"
        " exec "InstantMarkdownPreview"
    elseif &filetype == 'tex'
        silent! exec "VimtexStop"
        silent! exec "VimtexCompile"
    elseif &filetype == 'dart'
        exec "CocCommand flutter.run -d ".g:flutter_default_device." ".g:flutter_run_args
        silent! exec "CocCommand flutter.dev.openDevLog"
    elseif &filetype == 'javascript'
        :tabe %
        " :term node %
        :term export DEBUG="INFO,ERROR,WARNING"; node --trace-warnings %
    elseif &filetype == 'typescript'
        :tabe %
        " :term node %
        :term export DEBUG="INFO,ERROR,WARNING"; tsc %; node --trace-warnings %<.js
    elseif &filetype == 'go'
        " call system('st -e sh -c \"go run '.@%.'; echo; echo [ Process exit ]; read a"')
        " exec 'tabe'
        " exec 'term go run '.g:file_whole_name
        exec 'tabe %'
        exec 'term go run %'
    elseif &filetype == 'sql'
        exec 'tabe %'
        exec 'term gsql \-\d school -f %'
    endif
endfunc

function! ZzxRun()
    if &filetype == 'cpp'
        " call system('st -e sh -c \"~/Documents/cpptmp.exe; echo; echo; echo [process end]; read a"')
        exec "tabe"
        exec "term ~/Documents/Programming/Cpp/ctmp.exe"
    elseif &filetype == 'c'
        exec "tabe"
        exec "term ~/Documents/Programming/Cpp/ctmp.exe"
        " call system('st -e sh -c "~/Documents/ctmp.exe; echo; echo; echo [process end]; read a"')
    elseif &filetype == 'java'
        exec "!javac %"
        exec "!time java %<"
    elseif &filetype == 'asm'
        exec "!lc3as %<"
    elseif &filetype == 'sh'
        :!time bash %
    elseif &filetype == 'python'
        " set splitbelow
        " :sp
        exec "tabe %"
        :term python3 %
     " :term python3 %
    elseif &filetype == 'html'
        silent! exec "!".g:mkdp_browser." % &"
    elseif &filetype == 'markdown'
        silent exec "!google-chrome-stable %"
        " exec "InstantMarkdownPreview"
    elseif &filetype == 'tex'
        silent! exec "VimtexStop"
        silent! exec "VimtexCompile"
    elseif &filetype == 'dart'
        exec "CocCommand flutter.run -d ".g:flutter_default_device." ".g:flutter_run_args
        silent! exec "CocCommand flutter.dev.openDevLog"
    elseif &filetype == 'javascript'
        set splitbelow
        :sp
        :term export DEBUG="INFO,ERROR,WARNING"; node --trace-warnings .
    elseif &filetype == 'go'
        " call system('st -e sh -c \"go run '.@%.'; echo; echo [ Process exit ]; read a"')
        " exec 'tabe'
        " exec 'term go run '.g:file_whole_name
        exec 'tabe %'
        exec 'term go run %'
    endif
endfunc

exec 'highlight Cursor guibg=Red'


" ##### auto fcitx  ###########
" let g:input_toggle = 1
" function! Fcitx2en()
"    let s:input_status = system("fcitx5-remote")
"    if s:input_status == 2
"       let g:input_toggle = 1
"       let l:a = system("fcitx5-remote -c")
"    endif
" endfunction

" function! Fcitx2zh()
"    let s:input_status = system("fcitx5-remote")
"    if s:input_status != 2 && g:input_toggle == 1
"       let l:a = system("fcitx5-remote -o")
"       let g:input_toggle = 0
"    endif
" endfunction

" set ttimeoutlen=0
" 退出插入模式
" autocmd InsertLeave * call Fcitx2en()
" 进入插入模式
" autocmd InsertEnter * call Fcitx2zh()
" ##### auto fcitx end ######

" source ~/.config/nvim/map.vim
source ~/.config/nvim/plug.vim
source ~/.config/nvim/map.vim
